package com.qinggeng.project.system.service.impl;

import java.util.List;

import com.qinggeng.common.enums.CommonEnum;
import com.qinggeng.common.enums.ShowTypeEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.project.api.security.utils.ApiUtils;
import com.qinggeng.project.system.domain.JobUser;
import com.qinggeng.project.system.service.IJobUserService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qinggeng.project.system.mapper.JobInfoCollectMapper;
import com.qinggeng.project.system.domain.JobInfoCollect;
import com.qinggeng.project.system.service.IJobInfoCollectService;

import javax.annotation.Resource;

/**
 * 招聘信息收藏Service业务层处理
 *
 * @author smallfish
 * @date 2020-12-24
 */
@Service
public class JobInfoCollectServiceImpl implements IJobInfoCollectService
{
    @Resource
    private JobInfoCollectMapper jobInfoCollectMapper;
    @Autowired
    private IJobUserService jobUserService;

    /**
     * 查询招聘信息收藏
     *
     * @param collectId 招聘信息收藏ID
     * @return 招聘信息收藏
     */
    @Override
    public JobInfoCollect selectJobInfoCollectById(Long collectId)
    {
        return jobInfoCollectMapper.selectJobInfoCollectById(collectId);
    }


    public boolean checkJobInfoIsCollect(Long jobInfoId,String collectType){
        Long jobUserId=jobUserService.getJobUserId();
        JobInfoCollect where=new JobInfoCollect();
        where.setJobUserId(jobUserId);
        where.setJobInfoId(jobInfoId);
        where.setCollectType(collectType);
        JobInfoCollect jobInfoCollect=jobInfoCollectMapper.getJobInfoCollectByJobUserIdAndJobInfoIdAndCollectType(where);
        if(null==jobInfoCollect||null==jobInfoCollect.getStatus()){
            return false;
        }
        return jobInfoCollect.getStatus().equals(CommonEnum.STATUS_10.getValue());
    }

    /**
     * 查询招聘信息收藏列表
     *
     * @param jobInfoCollect 招聘信息收藏
     * @return 招聘信息收藏
     */
    @Override
    public List<JobInfoCollect> listJobInfoCollect(JobInfoCollect jobInfoCollect)
    {
        return jobInfoCollectMapper.selectJobInfoCollectList(jobInfoCollect);
    }

    /**
     * 新增招聘信息收藏
     *
     * @param jobInfoCollect 招聘信息收藏
     * @return 结果
     */
    @Override
    public boolean insertJobInfoCollect(JobInfoCollect jobInfoCollect) {
        jobInfoCollect.setStatus(CommonEnum.STATUS_10.getValue());
        jobInfoCollect.setCreateTime(DateUtils.getNowDate());
        return jobInfoCollectMapper.insertJobInfoCollect(jobInfoCollect)>0;
    }

    /**
     * 修改招聘信息收藏
     *
     * @param jobInfoCollect 招聘信息收藏
     * @return 结果
     */
    @Override
    public boolean updateJobInfoCollect(JobInfoCollect jobInfoCollect)
    {
        jobInfoCollect.setUpdateTime(DateUtils.getNowDate());
        return jobInfoCollectMapper.updateJobInfoCollect(jobInfoCollect)>0;
    }

    /**
     * 批量删除招聘信息收藏
     *
     * @param collectIds 需要删除的招聘信息收藏ID
     * @return 结果
     */
    @Override
    public int deleteJobInfoCollectByIds(Long[] collectIds)
    {
        return jobInfoCollectMapper.deleteJobInfoCollectByIds(collectIds);
    }

    /**
     * 删除招聘信息收藏信息
     *
     * @param collectId 招聘信息收藏ID
     * @return 结果
     */
    @Override
    public int deleteJobInfoCollectById(Long collectId)
    {
        return jobInfoCollectMapper.deleteJobInfoCollectById(collectId);
    }

    public boolean updateJobInfoCollect(Long jobUserId,Long jobInfoId,String collectType){

        JobInfoCollect jobInfoCollect=new JobInfoCollect();
        jobInfoCollect.setJobUserId(jobUserId);
        jobInfoCollect.setJobInfoId(jobInfoId);
        jobInfoCollect.setCollectType(collectType);
        JobInfoCollect oldCollect=jobInfoCollectMapper.getJobInfoCollectByJobUserIdAndJobInfoIdAndCollectType(jobInfoCollect);
        if(oldCollect!=null){
            return this.updateJobInfoCollectStatus(oldCollect);
        }
        return this.insertJobInfoCollect(jobInfoCollect);
    }

    public boolean updateJobInfoCollectStatus(JobInfoCollect jobInfoCollect){
        if(jobInfoCollect.getStatus().equals(CommonEnum.STATUS_10.getValue())){
            jobInfoCollect.setStatus(CommonEnum.STATUS_20.getValue());
        }else {
            jobInfoCollect.setStatus(CommonEnum.STATUS_10.getValue());
        }
        return this.updateJobInfoCollect(jobInfoCollect);
    }


}
